package com.jadcon.bedbreakfast.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Validator;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.jadcon.bedbreakfast.domain.Customer;
import com.jadcon.bedbreakfast.domain.CustomerService;
import com.jadcon.bedbreakfast.domain.PhoneNumber;
import com.jadcon.bedbreakfast.domain.Reservation;
import com.jadcon.bedbreakfast.domain.ReservationService;
import com.jadcon.bedbreakfast.domain.State;

@Controller
@SessionAttributes(value = {"newCustomer", "stateList", "phoneList"})
public class AdminController {

	@Autowired
	private ReservationService reservationService;
	@Autowired
	private CustomerService customerService;
	@Autowired
	Validator validator;
	
	public void setValidator(Validator validator) {
		this.validator = validator;
	}

	@RequestMapping("/customerInformation.html")
	public ModelAndView getCustomerInformation(@RequestParam String customerUsername) {
		
		String username = SecurityContextHolder.getContext().getAuthentication().getName();
		if (username.contentEquals("anonymousUser")){
			ModelAndView mav = new ModelAndView("redirect:bedbreakfast-webapp/spring_security_login");
			return mav;
		}
 
		Customer customer = customerService.findByUsername(customerUsername);
		List<Reservation> reservationList = reservationService.findByUsername(customerUsername);
		ModelAndView mav = new ModelAndView("customerInformation");
		mav.addObject("customer", customer);
		mav.addObject("reservationList", reservationList);
		return mav;

	}
	
	@RequestMapping("/editCustomer.html")
	public ModelAndView editCustomer(){
		
		String username = SecurityContextHolder.getContext().getAuthentication().getName();
		if (username.contentEquals("anonymousUser")){
			ModelAndView mav = new ModelAndView("redirect:bedbreakfast-webapp/spring_security_login");
			return mav;
		}
		
		Customer newCustomer = customerService.findByUsername(username);
		List<State> stateList = customerService.getStateList();
		List<PhoneNumber> phoneList = newCustomer.getPhoneNumberList();
		
		ModelAndView mav = new ModelAndView("editCustomer");
		mav.addObject("stateList", stateList);
		mav.addObject("newCustomer", newCustomer);
		mav.addObject("phoneList", phoneList);
		return mav;
		}
	
	
	@RequestMapping("/editedCustomerSaved.html")
	public ModelAndView editedCustomerSaved(
			@ModelAttribute("newCustomer") Customer newCustomer, BindingResult result) {
		
		validator.validate(newCustomer, result);

		if (result.hasErrors()) {
			ModelAndView mav = new ModelAndView("editCustomer");
			List<State> stateList = customerService.getStateList();
			mav.addObject("stateList", stateList);
			mav.addObject("newCustomer", newCustomer);
			return mav;
		}

		customerService.save(newCustomer);

		ModelAndView mav = new ModelAndView("createCustomerSaved");
		mav.addObject("newCustomer", newCustomer);
		return mav;
		
	}
	
	@RequestMapping("/viewAccounts.html")
	public ModelAndView viewAccounts(){
		
		List<Customer> customerList = customerService.getCustomerList();
		
		ModelAndView mav = new ModelAndView("viewAccounts");
		mav.addObject("customerList", customerList);
		return mav;
	}
	
	@RequestMapping("/reservationList")
	public ModelAndView reservationList() {
		
		String username = SecurityContextHolder.getContext().getAuthentication().getName();
		Customer customer = customerService.findByUsername(username);
		
		List<Reservation> reservationList = reservationService.getReservationList();
		ModelAndView mav = new ModelAndView("reservationList");
		mav.addObject("reservationList", reservationList);
		mav.addObject("customer", customer);
		return mav;
	}
	

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, false));
	}

}
